﻿//
// DirectXPage.xaml.cpp
// Implementation of the DirectXPage.xaml class.
//

#include "pch.h"
#include "DirectXPage.xaml.h"
#include "Graphics.h"

using namespace Direct2DDemo;

using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Graphics::Display;
using namespace Windows::UI::Input;
using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;


float xLoc;
float yLoc;
float xSpeed=3;
float ySpeed=0;
float yGravity=0.9;
DirectXPage::DirectXPage() :
	m_renderNeeded(true),
	m_lastPointValid(false)
{
	InitializeComponent();

	m_graphics=ref new Graphics();

	m_graphics->Initialize(
		Window::Current->CoreWindow,
		SwapChainPanel,
		DisplayProperties::LogicalDpi
		);

	m_graphics->RenderTarget+=ref new RenderHandler(this,&DirectXPage::Render);
	Window::Current->CoreWindow->SizeChanged += 
		ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &DirectXPage::OnWindowSizeChanged);

	DisplayProperties::LogicalDpiChanged +=
		ref new DisplayPropertiesEventHandler(this, &DirectXPage::OnLogicalDpiChanged);

	DisplayProperties::OrientationChanged +=
        ref new DisplayPropertiesEventHandler(this, &DirectXPage::OnOrientationChanged);

	DisplayProperties::DisplayContentsInvalidated +=
		ref new DisplayPropertiesEventHandler(this, &DirectXPage::OnDisplayContentsInvalidated);
	
	m_eventToken = CompositionTarget::Rendering::add(ref new EventHandler<Object^>(this, &DirectXPage::OnRendering));

}


void DirectXPage::OnPointerMoved(Object^ sender, PointerRoutedEventArgs^ args)
{

}

void DirectXPage::OnPointerReleased(Object^ sender, PointerRoutedEventArgs^ args)
{
	//m_graphics->GetBitmapImage();
}

void DirectXPage::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)
{
	m_graphics->UpdateForWindowSizeChange();
}

void DirectXPage::OnLogicalDpiChanged(Object^ sender)
{
	m_graphics->SetDpi(DisplayProperties::LogicalDpi);
}

void DirectXPage::OnOrientationChanged(Object^ sender)
{
	m_graphics->UpdateForWindowSizeChange();
}

void DirectXPage::OnDisplayContentsInvalidated(Object^ sender)
{
	m_graphics->ValidateDevice();
}

void DirectXPage::OnRendering(Object^ sender, Object^ args)
{
	Render();
}

void DirectXPage::Render()
{
	m_graphics->StartDraw();
	m_graphics->Clear(0x99ffffff);
	Point point(xLoc,yLoc);

	ySpeed+=yGravity;

	if(ySpeed>23)
		ySpeed=23;

	xLoc+=xSpeed;
	yLoc+=ySpeed;
	if(xLoc<0||xLoc>900)
		xSpeed=-xSpeed;
	if(yLoc<0)
		ySpeed=-ySpeed;
	if(yLoc>600)
	{
		yLoc=600;
		ySpeed=-ySpeed;
	}
	m_graphics->DrawRectangle(point.X,point.Y,100,50,0xff009900,0x99000088,2);
	//m_graphics->DrawText("Hello world",point.X,point.Y,100,100);
	m_graphics->EndDraw();
}


void DirectXPage::SaveInternalState(Windows::Foundation::Collections::IPropertySet^ state)
{

}

void DirectXPage::LoadInternalState(Windows::Foundation::Collections::IPropertySet^ state)
{

}